<template>
  <div id="userManagePage">
    <!-- 搜索表单 -->
    <div class="search-container">
      <div class="search-header">
        <div class="search-title">
          <span class="title-icon">👥</span>
          <span>用户管理</span>
        </div>
        <div class="search-subtitle">管理系统用户信息和权限</div>
      </div>
      <a-form layout="inline" :model="searchParams" @finish="doSearch" class="search-form">
        <a-form-item label="账号" class="form-item">
          <a-input
            v-model:value="searchParams.userAccount"
            placeholder="请输入用户账号"
            prefix="👤"
            class="search-input"
          />
        </a-form-item>
        <a-form-item label="用户名" class="form-item">
          <a-input
            v-model:value="searchParams.userName"
            placeholder="请输入用户名"
            prefix="📝"
            class="search-input"
          />
        </a-form-item>
        <a-form-item class="form-item">
          <a-button type="primary" html-type="submit" class="search-btn">
            <template #icon>
              <span>🔍</span>
            </template>
            搜索用户
          </a-button>
        </a-form-item>
      </a-form>
    </div>

    <!-- 表格 -->
    <!-- 表格容器 -->
    <div class="table-container">
      <a-table :columns="columns" :data-source="data" :pagination="pagination" @change="doTableChange" class="user-table">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'userAvatar'">
        <a-image :src="record.userAvatar" :width="120" />
      </template>
      <template v-else-if="column.dataIndex === 'userRole'">
        <div v-if="record.userRole === 'admin'">
          <a-tag color="green">管理员</a-tag>
        </div>
        <div v-else>
          <a-tag color="blue">普通用户</a-tag>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'createTime'">
        {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
      </template>
      <template v-else-if="column.dataIndex === 'updateTime'">
        {{ dayjs(record.updateTime).format('YYYY-MM-DD HH:mm:ss') }}
      </template>
      <template v-else-if="column.key === 'action'">
        <a-button danger @click="doDelete(record.id)">删除</a-button>
      </template>
      </template>
      </a-table>
    </div>
  </div>
</template>
<script lang="ts" setup>
// 移除未使用的图标导入
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUser, listUserVoByPage } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
// 获取数据
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]
// 数据
const data = ref<API.UserSafeVO[]>([])
const total = ref(0)
// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})
// 表格变化处理
const doTableChange = (page: any) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索条件
const searchParams = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

// 获取数据
const fetchData = async () => {
  const res = await listUserVoByPage({
    ...searchParams,
  })
  if (res.data.data) {
    data.value = res.data.data.records ?? []
    total.value = Number(res.data.data.total) ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}
// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteUser({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } else {
    message.error('删除失败')
  }
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

/* 统一动画变量 */
:root {
  --animation-duration-fast: 0.2s;
  --animation-duration-normal: 0.3s;
  --animation-duration-slow: 0.5s;
  --animation-easing: cubic-bezier(0.4, 0, 0.2, 1);
  --animation-easing-bounce: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

/* 动画定义 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 页面容器样式 */
#userManagePage {
  padding: 32px;
  background: rgba(248, 250, 252, 0.8);
  min-height: 100vh;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  animation: fadeInUp 0.8s var(--animation-easing);
}

/* 搜索容器样式 */
.search-container {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  padding: 28px;
  margin-bottom: 24px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: slideInLeft 0.6s var(--animation-easing);
  position: relative;
  overflow: hidden;
}

.search-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  border-radius: 20px 20px 0 0;
}

.search-header {
  text-align: center;
  margin-bottom: 24px;
}

.search-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 22px;
  font-weight: 700;
  color: #1a202c;
  margin-bottom: 6px;
}

.title-icon {
  font-size: 24px;
}

.search-subtitle {
  font-size: 14px;
  color: #64748b;
  font-weight: 500;
  opacity: 0.8;
}

.search-form {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 16px;
}

.form-item {
  margin-bottom: 0;
}

.search-input {
  width: 200px;
  height: 40px;
  border-radius: 12px;
  border: 2px solid rgba(226, 232, 240, 0.8);
  transition: all var(--animation-duration-normal) var(--animation-easing);
  font-weight: 500;
}

.search-input:hover {
  border-color: rgba(102, 126, 234, 0.4);
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.05);
}

.search-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

.search-btn {
  height: 40px;
  padding: 0 24px;
  border-radius: 12px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

.search-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
}

/* 表格容器样式 */
.table-container {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: fadeInUp 0.6s var(--animation-easing) 0.2s both;
  position: relative;
  overflow: hidden;
}

.table-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #10b981 0%, #059669 50%, #047857 100%);
  border-radius: 20px 20px 0 0;
}

/* 表格样式 */
.user-table {
  background: transparent;
  border-radius: 16px;
}

:deep(.ant-table-thead > tr > th) {
  background: rgba(102, 126, 234, 0.05);
  border-bottom: 1px solid rgba(102, 126, 234, 0.1);
  font-weight: 600;
  color: #374151;
  font-size: 14px;
}

:deep(.ant-table-tbody > tr) {
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-table-tbody > tr:hover) {
  background: rgba(102, 126, 234, 0.02);
  transform: translateX(2px);
}

:deep(.ant-table-tbody > tr > td) {
  vertical-align: middle;
  border-bottom: 1px solid rgba(229, 231, 235, 0.5);
  font-weight: 500;
  color: #4b5563;
}

/* 头像样式 */
:deep(.ant-avatar) {
  border: 2px solid rgba(102, 126, 234, 0.2);
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-avatar):hover {
  transform: scale(1.1);
  border-color: rgba(102, 126, 234, 0.4);
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.2);
}

/* 按钮样式 */
:deep(.ant-btn) {
  border-radius: 8px;
  font-weight: 500;
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-btn:hover) {
  transform: translateY(-1px);
}

:deep(.ant-btn-dangerous) {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  border: none;
  color: white;
  box-shadow: 0 4px 15px rgba(239, 68, 68, 0.3);
}

:deep(.ant-btn-dangerous):hover {
  background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
  box-shadow: 0 6px 20px rgba(239, 68, 68, 0.4);
  transform: translateY(-2px);
}

/* 分页样式 */
:deep(.ant-pagination) {
  margin-top: 24px;
  text-align: center;
}

:deep(.ant-pagination-item) {
  border-radius: 8px;
  border: 1px solid rgba(209, 213, 219, 0.8);
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-pagination-item:hover) {
  border-color: #667eea;
  transform: translateY(-1px);
}

:deep(.ant-pagination-item-active) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
}

:deep(.ant-pagination-item-active a) {
  color: white;
  font-weight: 600;
}

/* 用户角色标签样式 */
:deep(.ant-tag) {
  border-radius: 12px;
  font-weight: 500;
  padding: 4px 12px;
  border: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  :deep(.ant-layout-content) {
    padding: 20px 16px;
  }

  :deep(.ant-table) {
    font-size: 12px;
  }
}
</style>
